/*
 *  Licensed to GraphHopper GmbH under one or more contributor
 *  license agreements. See the NOTICE file distributed with this work for
 *  additional information regarding copyright ownership.
 *
 *  GraphHopper GmbH licenses this file to you under the Apache License,
 *  Version 2.0 (the "License"); you may not use this file except in
 *  compliance with the License. You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package com.graphhopper.routing.util.parsers;

import com.graphhopper.reader.ReaderRelation;
import com.graphhopper.reader.ReaderWay;
import com.graphhopper.routing.ev.*;
import com.graphhopper.routing.util.EncodingManager;
import com.graphhopper.routing.util.OSMParsers;
import com.graphhopper.routing.util.PriorityCode;
import com.graphhopper.storage.IntsRef;
import com.graphhopper.util.PMap;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

class TagParsingTest {

    @Test
    public void testSharedEncodedValues() {
        BooleanEncodedValue carAccessEnc = VehicleAccess.create("car");
        BooleanEncodedValue footAccessEnc = VehicleAccess.create("foot");
        BooleanEncodedValue bikeAccessEnc = VehicleAccess.create("bike");
        BooleanEncodedValue mtbAccessEnc = VehicleAccess.create("mtb");
        List<BooleanEncodedValue> accessEncs = Arrays.asList(carAccessEnc, footAccessEnc, bikeAccessEnc, mtbAccessEnc);
        EncodingManager manager = EncodingManager.start()
                .add(carAccessEnc).add(VehicleSpeed.create("car", 5, 5, true))
                .add(footAccessEnc).add(VehicleSpeed.create("foot", 4, 1, true)).add(VehiclePriority.create("foot", 4, PriorityCode.getFactor(1), false))
                .add(bikeAccessEnc).add(VehicleSpeed.create("bike", 4, 2, false)).add(VehiclePriority.create("bike", 4, PriorityCode.getFactor(1), false))
                .add(mtbAccessEnc).add(VehicleSpeed.create("mtb", 4, 2, false)).add(VehiclePriority.create("mtb", 4, PriorityCode.getFactor(1), false))
                .add(RouteNetwork.create(FootNetwork.KEY))
                .add(RouteNetwork.create(BikeNetwork.KEY))
                .add(Roundabout.create())
                .add(Smoothness.create())
                .build();

        BooleanEncodedValue roundaboutEnc = manager.getBooleanEncodedValue(Roundabout.KEY);
        List<TagParser> tagParsers = Arrays.asList(
                new OSMRoundaboutParser(roundaboutEnc),
                new CarAccessParser(manager, new PMap()),
                new FootAccessParser(manager, new PMap()),
                new BikeAccessParser(manager, new PMap()),
                new MountainBikeAccessParser(manager, new PMap())
        );

        final ArrayEdgeIntAccess intAccess = ArrayEdgeIntAccess.createFromBytes(manager.getBytesForFlags());
        int edgeId = 0;
        IntsRef relFlags = manager.createRelationFlags();
        ReaderWay way = new ReaderWay(1);
        way.setTag("highway", "primary");
        way.setTag("junction", "roundabout");
        tagParsers.forEach(p -> p.handleWayTags(edgeId, intAccess, way, relFlags));

        assertTrue(roundaboutEnc.getBool(false, edgeId, intAccess));
        for (BooleanEncodedValue accessEnc : accessEncs)
            assertTrue(accessEnc.getBool(false, edgeId, intAccess));

        way.clearTags();
        way.setTag("highway", "tertiary");
        way.setTag("junction", "circular");
        tagParsers.forEach(p -> p.handleWayTags(edgeId, intAccess, way, relFlags));

        assertTrue(roundaboutEnc.getBool(false, edgeId, intAccess));
        for (BooleanEncodedValue accessEnc : accessEncs)
            assertTrue(accessEnc.getBool(false, edgeId, intAccess));
    }

}
